package com.example.loadpicture;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;

/**
 * 加载大图片
 */
public class MainActivity extends Activity {
    private ImageView iv_img;
    private int screenWidth;
    private int screenHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_img = (ImageView) findViewById(R.id.iv_img);


        //屏幕的管理者对象
        WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);

        //获得屏幕的宽高
//        wm.getDefaultDisplay().getWidth();
//        wm.getDefaultDisplay().getHeight();

        //新方法获得屏幕的宽高
        Point point = new Point();
        wm.getDefaultDisplay().getSize(point);

        //获得屏幕的宽高
        screenWidth = point.x;
        screenHeight = point.y;
    }

    public void click(View view) {

//        Bitmap bitmap =
//                BitmapFactory.decodeFile("/mnt/sdcard/dog.bmp");
//
//        iv_img.setImageBitmap(bitmap);

        BitmapFactory.Options options = new BitmapFactory.Options();

        //返回一个没有图片的Bitmap ,不真正解析位图 只加载配置信息
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFile("/mnt/sdcard/dog.bmp", options);


        //拿到图片的尺寸
        int imgWidth = options.outWidth;
        int imgHeight = options.outHeight;

        //计算缩放比例
        //默认的比例 为1
        int scale = 1;
        //宽度的比例
        int scaleX = imgWidth / screenWidth;
        //高度的比例
        int scaleY = imgHeight / screenHeight;

        //宽和高 谁的比例大 使用谁的比例

        if (scaleX > scaleY && scaleX > scale) {
            scale = scaleX;
        }

        if (scaleY > scaleX && scaleY > scale) {
            scale = scaleY;
        }

        //设置图片的比例
        options.inSampleSize = scale;

        //开始解析位图 加载图片像素信息
        options.inJustDecodeBounds = false;
        //缩放之后的图片
        Bitmap bitmap = BitmapFactory.decodeFile("/mnt/sdcard/dog.bmp", options);

        iv_img.setImageBitmap(bitmap);

    }
}
